package com.modifiertech.lineage.client.gui.ftbint;

import com.feed_the_beast.ftblib.lib.gui.*;
import com.feed_the_beast.ftblib.lib.gui.misc.GuiEditConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.resources.I18n;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GuiLineageV2 extends GuiBase {
    public  PanelWarpper<PanelSkillV2> panelSkillV2w;
    public PanelFootV2 panelFootV2w;
    public PanelFootConsole panelFootConsole;
    public PanelSkillDescV2 panelSkillDescV2;
    public PanelHeadV2 panelHeadV2;
    public  PanelAttrV2 panelAttrV2;
    private PanelSkillV2 panelSkillV2;
    private  Button close;

    public GuiLineageV2() {
        init();
    }

    public PanelSkillV2 getPanelSkillV2() {
        return panelSkillV2;
    }

    public void setPanelSkillV2(PanelSkillV2 panelSkillV2) {
        this.panelSkillV2 = panelSkillV2;
    }

    public void init(){
        initHeadPanel();
        initFootPanelW();
        initFootConsole();
        initSkillPanelW();
        initSkillDescPanel();
        initAttrPanel();
        initBtn();
    }



    private void initAttrPanel() {
        this.setPanelAttrV2(new PanelAttrV2(this));
    }

    private void initSkillDescPanel() {
        this.setPanelSkillDescV2(new PanelSkillDescV2(this,this, (PanelSkillV2) panelSkillV2w.getInnerPanelRel()));
    }


    private void initHeadPanel() {
        this.setPanelHeadV2(new PanelHeadV2(this,this));
    }

    public void initSkillPanelW(){
        PanelWarpper panelWarpper=new PanelWarpper(this);
        panelWarpper.doInit(new PanelSkillV2(panelWarpper,panelFootV2w,this));
        this.setPanelSkillV2w(panelWarpper);
        this.setPanelSkillV2((PanelSkillV2) panelWarpper.getInnerPanelRel());
    }
    public void initFootPanelW(){
        PanelFootV2 panelFootV2=new PanelFootV2(this,this);
        this.setPanelFootV2w(panelFootV2);
    }
    public void initFootConsole(){
        PanelFootConsole panelFootConsole=new PanelFootConsole(this,this);
        this.setPanelFootConsole(panelFootConsole);
    }

    public PanelFootConsole getPanelFootConsole() {
        return panelFootConsole;
    }

    public void setPanelFootConsole(PanelFootConsole panelFootConsole) {
        this.panelFootConsole = panelFootConsole;
    }

    //    public void initSkillPanel(){
//        this.setPanelSkillV2(new PanelSkillV2(this));   //init skill panel
//        //setpos
//        this.getPanelSkillV2().setUnicode(true);
//    }
    public void initBtn(){
        close = new SimpleButton(this, I18n.format("gui.close"), GuiIcons.CLOSE, (widget, button) -> widget.getGui().closeGui());
    }

    @Override
    public void addWidgets() {
        add(getPanelSkillV2w());
        add(getPanelSkillDescV2());
        add(getPanelAttrV2());
        add(getPanelFootV2w());
        add(getPanelFootConsole());
        add(getPanelHeadV2());
        add(getClose());
    }

    @Override
    public void alignWidgets() {
//        this.getPanelSkillV2w().alignWidgets();
//        this.getPanelAttrV2().alignWidgets();
//        this.getPanelFootV2w().alignWidgets();
//        this.getPanelFootConsole().alignWidgets();
//        this.getPanelHeadV2().alignWidgets();
//        this.getPanelSkillDescV2().alignWidgets();
//        int panelskillWidth=width*2/3-10;
//        int panelskillHeight=height*2/3-10;
//
//        this.getPanelSkillV2w().setPosAndSize(9,33,panelskillWidth,panelskillHeight);
//
//        this.getPanelHeadV2().setPosAndSize(11,8,width-40,20);
//        this.getPanelAttrV2().setPosAndSize(9+panelskillWidth+10,33,width-40-panelskillWidth,(panelskillHeight*3/5));
//        this.getPanelSkillDescV2().setPosAndSize(9+panelskillWidth+10,33+(panelskillHeight*3/5)+3,width-40-panelskillWidth,panelskillHeight-(panelskillHeight*3/5));
//        this.getPanelFootV2w().setPosAndSize(11,33+panelskillHeight+5,panelskillWidth,height/4);
//        this.getPanelFootConsole().setPosAndSize(9+panelskillWidth+10,33+panelskillHeight+5,width-40-panelskillWidth,height/4);
//        this.getClose().setPosAndSize(width-24,8,20,20);
        // 获取当前屏幕的分辨率
        ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft());
        int screenWidth = scaledResolution.getScaledWidth();
        int screenHeight = scaledResolution.getScaledHeight();

        // 定义安全边距
        int paddingBottom = 10;  // 底部边距
        int paddingSide = 10;    // 左右侧边距
        int paddingTop = 33;     // 顶部边距

        // 动态计算面板的宽度和高度，确保自适应窗口大小
        int panelskillWidth = (screenWidth * 2 / 3) - paddingSide;
        int panelskillHeight = (screenHeight * 2 / 3) - paddingTop;

        // 首先对所有的面板调用 alignWidgets
        this.getPanelSkillV2w().alignWidgets();
        this.getPanelAttrV2().alignWidgets();
        this.getPanelFootV2w().alignWidgets();
        this.getPanelFootConsole().alignWidgets();
        this.getPanelHeadV2().alignWidgets();
        this.getPanelSkillDescV2().alignWidgets();

        // 设置各个面板的位置和大小
        // PanelSkillV2w: 位于屏幕左侧的技能面板
        this.getPanelSkillV2w().setPosAndSize(paddingSide, paddingTop, panelskillWidth, panelskillHeight);

        // PanelHeadV2: 位于顶部的头部面板，宽度覆盖整个屏幕（减去边距），高度固定
        this.getPanelHeadV2().setPosAndSize(paddingSide + 2, paddingTop - 25, screenWidth - 40, 20);

        // PanelAttrV2: 位于右侧的属性面板，动态高度，基于 PanelSkillV2w 的高度
        this.getPanelAttrV2().setPosAndSize(panelskillWidth + paddingSide + 10, paddingTop, screenWidth - 40 - panelskillWidth, (panelskillHeight * 3 / 5));

        // PanelSkillDescV2: 紧接 PanelAttrV2 下面的技能描述面板，动态高度
        this.getPanelSkillDescV2().setPosAndSize(
                panelskillWidth + paddingSide + 10, // X 轴位置
                paddingTop + (panelskillHeight * 3 / 5) + 3, // Y 轴位置
                screenWidth - 40 - panelskillWidth, // 宽度
                panelskillHeight - (panelskillHeight * 3 / 5) // 高度
        );

        // PanelFootV2w: 位于底部的面板，左侧，占用高度的1/4，确保距离底部有 paddingBottom
        this.getPanelFootV2w().setPosAndSize(
                paddingSide, // X 轴位置
                screenHeight - screenHeight / 4 - paddingBottom, // Y 轴位置，确保距离底部有 paddingBottom
                panelskillWidth, // 宽度
                screenHeight / 4 // 高度
        );

        // PanelFootConsole: 位于底部的面板，右侧，跟 PanelFootV2w 对应，确保距离底部有 paddingBottom
        this.getPanelFootConsole().setPosAndSize(
                panelskillWidth + paddingSide + 10, // X 轴位置
                screenHeight - screenHeight / 4 - paddingBottom, // Y 轴位置，确保距离底部有 paddingBottom
                screenWidth - 40 - panelskillWidth, // 宽度
                screenHeight / 4 // 高度
        );

        // Close Button: 关闭按钮，位于右上角
        this.getClose().setPosAndSize(screenWidth - 24, paddingTop - 2, 20, 20);
    }

    @Override
    public boolean onInit() {
        return setFullscreen();
    }

    @Override
    public void drawBackground(Theme theme, int x, int y, int w, int h) {
//        GuiEditConfig.COLOR_BACKGROUND.draw(0, 0, w, 20);
//        ImageBackGroundV2.IMAGE_SKILL_BACKGROUND.draw(x, y, w, h);

    }
    @Override
    public Theme getTheme()
    {
        return GuiEditConfig.THEME;
    }
























//    public PanelSkillV2 getPanelSkillV2() {
//        return panelSkillV2;
//    }
//
//    public void setPanelSkillV2(PanelSkillV2 panelSkillV2) {
//        this.panelSkillV2 = panelSkillV2;
//    }

    public Button getClose() {
        return close;
    }

    public void setClose(Button close) {
        this.close = close;
    }

    public PanelSkillDescV2 getPanelSkillDescV2() {
        return panelSkillDescV2;
    }

    public void setPanelSkillDescV2(PanelSkillDescV2 panelSkillDescV2) {
        this.panelSkillDescV2 = panelSkillDescV2;
    }

    public PanelHeadV2 getPanelHeadV2() {
        return panelHeadV2;
    }

    public void setPanelHeadV2(PanelHeadV2 panelHeadV2) {
        this.panelHeadV2 = panelHeadV2;
    }


    public void setPanelAttrV2(PanelAttrV2 panelAttrV2) {
        this.panelAttrV2 = panelAttrV2;
    }

    public PanelAttrV2 getPanelAttrV2() {
        return panelAttrV2;
    }

    public PanelWarpper getPanelSkillV2w() {
        return panelSkillV2w;
    }

    public void setPanelSkillV2w(PanelWarpper panelSkillV2w) {
        this.panelSkillV2w = panelSkillV2w;
    }

    public PanelFootV2 getPanelFootV2w() {
        return panelFootV2w;
    }

    public void setPanelFootV2w(PanelFootV2 panelFootV2w) {
        this.panelFootV2w = panelFootV2w;
    }

    public void addLineToConsolePanel(String consoleLineString){
        List<String> strings= new ArrayList<>(Arrays.asList(getPanelFootConsole().getTextField().text));
        strings.add(consoleLineString);
        StringBuffer stringBuffer=new StringBuffer();
        strings.forEach(h->stringBuffer.append(h).append("\n"));
        getPanelFootConsole().getTextField().setText(stringBuffer.toString());
        getPanelFootConsole().alignWidgets();
    }



}
